define(['jquery','api','user','util','text!pages/user/index.html','Vue'],function($,api,user,util){
	var $loginForm,
	    vm,
	    links=[
	      ['ticket','#/coupon/list','我的赢家券'],
	      ['person','#/user/card','经纪人'],
	      ['orderHistory','#/order/history','交易历史'],
	      //['points','#/order/points','我的积分'],
	      ['detail','#/fund/flow','收支明细'],
	      ['card','#/bank/list','我的银行卡'],
	      //['rule','#/article/rule','交易规则'],
	      ['share','#/user/share','我要分享'],
	      ['person','#/user/detail','修改密码'],
	      ['logout','#/logout','退出']
	    ],
	    groups=[];
	var controller={
	   init:function (){
	   	console.log(123);
	   	   var that=this;
		   if(!user.checkLogin())return;
		   api.initFooter(3);
		   api.setTitle('个人中心');
		   if(vm){
		      vm.show=true;
		   }else{
		   	  this.createGroup();
		      that.render();
		   }
		   api.userInfo(function(rs){
		   	   if(rs.retcode=='0'){
		   	   	  vm.user.fund=rs.userfund;
		   	   	  vm.user.username=rs.userfund.username;
		   	   	  user.data.uid=rs.userfund.uid;
		   	   	  user.data.username=rs.userfund.username;
		   	   	  user.data.mobile=rs.userfund.mobile ? rs.userfund.mobile : $.cookie('mobile');
		   	   }else{
		   	      util.msg('用户信息获取失败！');
		   	   }
		   });
		   
	    },
	    render:function(){
	       var userdata=user.data;
	       userdata.fund={};
	       $('#pages').append('<div id="user"></div>');
	       require(['text!pages/user/index.html','Vue'],function(tpl,Vue){
	       	    vm=new Vue({
	       	         el:'#user',
	       	         data:{
	       	            user:userdata,
	       	            show:true,
	       	            groups:groups
	       	         },
	       	         filters:{
	       	         	priceFormat:function(v){
	       	         	    return v?util.priceFormat(2,v):'';
	       	         	}
	       	         },
	       	         template:tpl
	       	    });
		   })
	    },
	    createGroup:function(){
	    	var gid=0,
	    	    len,
	    	    c;
	    	for(var i=0;i<links.length;i++){
	    		if(i&&i%3==0){
	    		   gid++
	    		}
	    		if(groups[gid]){
	    		   groups[gid].push(links[i]);
	    		}else{
	    		   groups[gid]=[links[i]];
	    		}
	    	}
	    	c=3-i%3;
	    	len=groups.length;
	    	if(c)for(var i=0;i<c;i++){
	    	    groups[len-1].push([])
	    	}
	    },
	    onRouteChange:function(){
	    	if(vm)vm.show=false;
	    }
	}
    return controller;
})
